//
//  ToastModifier.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/15.
//

import SwiftUI
import Kingfisher
import Combine

//给View新增扩展方法
extension View {
    public func showVoice(isPresent:Binding<Bool>, bean: VoiceCallParam) -> some View {
        //modifier 给当前试图进行修改
        modifier(VoiceCallViewModifier(isPresent: isPresent, bean: bean))
    }
}

/**
 * 试图修改器
 * VoiceCallView
 */
struct VoiceCallViewModifier: ViewModifier {
    @Binding var isPresent: Bool
    var bean: VoiceCallParam

    func body(content: Content) -> some View {
        ZStack {
            content.blur(radius: isPresent ? 5 : 0) // 设置模糊半径为 5
            if isPresent {
                VoiceCallView(bean: bean)
            }
        }
    }
    
}

//语音来电
struct VoiceCallView: View {
    @State private var secondsElapsed: Int = 0
    @State private var timer: Timer?
    @State var isAccept = false
    @State var isPlayHangUp = false //播放挂断语音
    var bean: VoiceCallParam

    var body: some View {
        VStack{
            if !isAccept{
                Text("语音来电")
                    .font(.system(size: 14))
                    .padding(.top, 56)
                    .foregroundStyle(.white)
                    .onAppear{
                        if bean.startBgVoice {
                            //播放来电声音
                            AudioPlayerManager.shared.playLocal(forResource:"voice",ofType:"mp3", looper : true)
                        }
                    }
            } else {
                Text(TimeUtils.formatTime(seconds: secondsElapsed))
                    .font(.system(size: 14))
                    .padding(.top, 56)
                    .foregroundStyle(.white)
                    .onAppear{
                        AudioPlayerManager.shared.pause()//暂停来电声音
                        timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
                            secondsElapsed += 1
                        }
                        //播放音频
                        if let data = bean.hexData {
                            AudioPlayerManager.shared.playData(from: data)
                            AudioPlayerManager.shared.onPlaybackFinished = {
                                //音频播放结束，播放一段结束的声音，然后关闭
                                if !isPlayHangUp {
                                    isPlayHangUp = true
                                    AudioPlayerManager.shared.playLocal(forResource:"hang_up",ofType:"mp3")
                                } else {
                                    commonManager.hideVoiceCall()
                                }
                            }
                        }
                    }
            }
            KFImage(URL(string: bean.logo.formatCdn()))
                .resizable()
                .placeholder{
                    Image(.appDefaultLogo)
                        .resizable()
                        .scaledToFill()
                        .padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20))
                        .frame(width: 98,height: 98)
                        .clipShape(Circle())
                }
                .background(Color.hex("515151").opacity(0.1))
                .scaledToFill()
                .frame(width: 98,height: 98)
                .clipShape(Circle())
                .padding(.top, 100)
            Text(bean.showName ?? "")
                .font(.system(size: 20))
                .fontWeight(.bold)
                .padding(.top, 22)
                .foregroundStyle(.white)
            Spacer()
            HStack{
                Spacer()
                VStack{
                    Image(.homeVoiceIconCallReject)
                        .resizable()
                        .frame(width: 72,height: 72)
                    Text(isAccept == true ? "挂断" : "拒绝")
                        .font(.system(size: 12))
                        .fontWeight(.bold)
                        .padding(.top, 8)
                        .foregroundStyle(.white)
                }.contentShape(Rectangle())
                    .onTapGesture {
                        commonManager.hideVoiceCall()
                    }
                if !isAccept{
                    Spacer()
                    Spacer()
                    VStack{
                        Image(.homeVoiceIconCallAccept)
                            .resizable()
                            .frame(width: 72,height: 72)
                        Text("接听")
                            .font(.system(size: 12))
                            .fontWeight(.bold)
                            .padding(.top, 8)
                            .foregroundStyle(.white)
                    }.contentShape(Rectangle())
                        .onTapGesture {
                            withAnimation{
                                isAccept = true
                            }
                        }
                }
                Spacer()
            }.padding(.bottom, 64)
            
        }
        .frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .top)
        .background(.black.opacity(0.6))
        .contentShape(Rectangle())
        .onAppear{
            isAccept = bean.isStart
        }
        .onDisappear {
            AudioPlayerManager.shared.pause()//暂停播放
            timer?.invalidate()
        }
        .onTapGesture {
            //防止透传
        }
    }
}

//#Preview {
//    VoiceCallView()
//}
//定义预览提供者
struct VoiceCallView_Previews: PreviewProvider {
    
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: VoiceCallView(bean: VoiceCallParam(showName: "我是名字")))
    }
}

